package com.qfmy.web.entity.Operate;

import com.qfmy.baseCode.bean.TableInfo;
import com.qfmy.baseCode.builder.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author: 廖志伟
 * @date: 2024-11-28
 * 生成代码的实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("all")
public class AutoCode {
    // 登录操作
    private OperateLogin operateLogin;
    //实体类操作
    private OperateBean operateBean;
    // 字段操作
    private OperateTableAndFiled operateTableAndFiled;

    /**
     * 执行生成代码
     */
    @SneakyThrows
    public void  execute(@NotNull List<TableInfo> tables) {
        //创建pojo对象
        BuilderPo builderPo = new BuilderPo(operateBean.getBasePackage() + "." + operateBean.getEntityPackage(), operateBean.getIsCommonFiled());
        //创建Query类
        BuilderQuery builderQuery = new BuilderQuery(operateBean.getBasePackage() + "." + operateBean.getQueryPackage(),operateTableAndFiled.getBeanQuery(),operateTableAndFiled.getLikeSearch(),operateLogin.getLoginTable());
        //创建Mapper接口
        BuilderMapper builderMapper = new BuilderMapper(operateBean.getBasePackage());
        //创建Mapper.xml
        BuilderMapperXml builderMapperXml = new BuilderMapperXml();
        //设置字段
        builderMapperXml.setBasePackage(operateBean.getBasePackage());
        builderMapperXml.setLikeSuffix(operateTableAndFiled.getLikeSearch());
        builderMapperXml.setPojoPackage(operateBean.getBasePackage() + "." + operateBean.getEntityPackage());
        //创建Service接口
        BuilderService builderService = new BuilderService(operateBean.getBasePackage(),operateBean.getBasePackage() + "." + operateBean.getEntityPackage(),operateBean.getBasePackage() + "." + operateBean.getQueryPackage());
        //创建Service实现类
        BuilderServiceImpl builderServiceImpl = new BuilderServiceImpl(operateBean.getBasePackage(),operateBean.getBasePackage() + "." + operateBean.getEntityPackage(),operateBean.getBasePackage() + "." + operateBean.getQueryPackage(),operateBean.getCommonPackage());
        //创建Controller类
        BuilderController builderController = new BuilderController(operateBean.getBasePackage() + "." + operateBean.getEntityPackage(),operateBean.getBasePackage() + "." + operateBean.getQueryPackage(),operateBean.getBasePackage(),operateBean.getCommonPackage());
        //创建文件上传的类
        BuilderFileUpload builderFileUpload = new BuilderFileUpload(operateBean.getCommonPackage(),operateBean.getBasePackage());
        //创建一个线程
        new Thread(() -> {
            //循环遍历表
            tables.stream().forEach(table ->
            {
                //创建pojo对象
                builderPo.execute(table);
                //创建Query类
                builderQuery.execute(table);
                if(operateLogin.getIsLogin() && !table.equals(operateLogin.getLoginTable()) && !table.equals("login_user"))
                {
                    //创建Mapper接口
                    builderMapper.execute(table);
                    //判断是生成Mapper.xml还是基于注解的方式
                    if (operateTableAndFiled.getSqlType().equals("xml")) {
                        //创建Mapper.xml
                        builderMapperXml.execute(table);
                    } else {
                        //TODO: 生成基于注解的Mapper,暂时未实现
                    }
                    //创建Service接口
                    builderService.execute(table);
                    //创建ServiceImpl类
                    builderServiceImpl.execute(table);
                    //创建Controller类
                    builderController.execute(table);
                }
            });
        }).run();
        //创建基础类
        BuilderBase builderBase = new BuilderBase(operateBean.getBasePackage() + "." + operateBean.getQueryPackage(),operateLogin.getLoginType(),operateLogin.getIsRedis(),operateLogin.getIsLogin(),operateBean.getBasePackage(),operateBean.getCommonPackage(),operateLogin.getRegisterType());
        //执行生成基础类
        builderBase.execute();

        //设置字段
        //判断是否有minio配置
        if(operateLogin.getImgType().equals("minio"))
        {
            //创建文件上传类
            builderFileUpload.execute();
        }
        //判断是否有登入请求
        if( operateLogin.getIsLogin())
        {
            //创建登入请求类
            BuildLogin buildLogin = new BuildLogin(operateLogin.getLoginPath(),operateBean.getBasePackage(),operateLogin.getLoginTable(),operateLogin.getRegisterType(),operateBean.getCommonPackage(),operateLogin.getLoginType(),operateBean.getEntityPackage(),operateBean.getQueryPackage(),operateTableAndFiled.getBeanQuery());
            buildLogin.execute();
        }
        //创建启动类
        BuilderConfig builderConfig = new BuilderConfig(operateLogin.getLoginType(),operateLogin.getImgType(),operateBean.getBasePackage(),operateBean.getBaseProjectName(),operateLogin.getLoginPath(),operateBean.getCommonPackage(),operateBean.getBasePackage(),operateBean.getCommonProjectName(),operateBean.getParentName(),operateBean.getParentPackage(),operateLogin.getRegisterType());
        builderConfig.execute();
        System.out.println("生成完成");
    }



}
